#include "signin.h"
#include "ui_signin.h"
#include"mainwindow.h"
#include"users.h"
#include<QMessageBox>

signin::signin(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::signin)
{
    ui->setupUi(this);

    ui->signupLabel->hide();
    ui->pswdcon_label->hide();
    ui->pswdconLineEdit->hide();
    ui->addressLineEdit->hide();
    ui->adress_label->hide();
    ui->returnsigninButton->hide();
    ui->signupButton->hide();
}

signin::~signin()
{
    delete ui;
}





//跳到注册按钮
void signin::on_returnsignupButton_clicked()
{
    ui->signin_label->hide();
    ui->returnsignupButton->hide();
    ui->confirmButton->hide();

    ui->signupLabel->show();
    ui->pswdcon_label->show();
    ui->pswdconLineEdit->show();
    ui->addressLineEdit->show();
    ui->adress_label->show();
    ui->returnsigninButton->show();
    ui->signupButton->show();

}

//跳到登录按钮
void signin::on_returnsigninButton_clicked()
{
    ui->signin_label->show();

    ui->signupLabel->hide();
    ui->pswdcon_label->hide();
    ui->pswdconLineEdit->hide();
    ui->addressLineEdit->hide();
    ui->adress_label->hide();
    ui->returnsigninButton->hide();
    ui->signupButton->hide();

    ui->returnsignupButton->show();
    ui->confirmButton->show();
}


//确认注册按钮
void signin::on_signupButton_clicked()
{
    // 获取输入
    QString phone = ui->telephoneLineEdit->text();
    QString password = ui->pswdLineEdit->text();
    QString confirmPassword = ui->pswdconLineEdit->text();
    QString address = ui->addressLineEdit->text();

    // 创建用户对象
    User newUser(phone, password, address);

    // 验证输入
    if (!newUser.isValid()) {
        QMessageBox::warning(this, "错误", "信息不能为空");
        return;
    }
    if (!newUser.isPasswordMatch(confirmPassword)) {
        QMessageBox::warning(this, "错误", "两次密码不一致");
        return;
    }
    if (!newUser.isPhoneValid()) {
        QMessageBox::warning(this, "错误", "请输入有效的11位手机号");
        return;
    }

    // 新增用户存在检查
    if (User::userExists(phone, "users.dat")) {
        QMessageBox::warning(this, "错误", "该手机号已注册");
        return;
    }

    // 保存用户数据
    if (User::saveToFile(newUser, "users.dat")) {
        QMessageBox::information(this, "成功", "注册成功");
        on_returnsigninButton_clicked(); // 返回登录界面
    } else {
        QMessageBox::critical(this, "错误", "保存用户数据失败");
    }
}

//确认登录按钮
void signin::on_confirmButton_clicked()
{
    QString phone = ui->telephoneLineEdit->text();
    QString password = ui->pswdLineEdit->text();

    // 输入非空检查
    if (phone.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this,"错误","信息不能为空");
        return;
    }

    // 从文件加载用户
    User user = User::loadFromFile(phone, "users.dat");

    // 验证用户
    if(!user.isValid()){
        QMessageBox::warning(this,"错误","用户不存在");
        return;
    }
    if (user.getPassword() != password) {
        QMessageBox::warning(this, "错误", "密码错误");
        return;
    }

    //登录成功,并把地址传进
    MainWindow* mainwindow= new MainWindow(user.getAddress());
    this->hide();
    mainwindow->show();

}

